package leetcode.code1706;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int[] findBall(int[][] grid) {
		int n = grid[0].length;
		int[] ans = new int[n];
		for (int i = 0; i < n; i++) {
			this.fall(grid, 0, i, 0, i, ans);
		}
		return ans;
	}

	private void fall(int[][] grid, int x, int y, int direction, int start, int[] ans) {
		if (y < 0 || y >= grid[0].length || (x < grid.length && grid[x][y] == -direction)) {
			ans[start] = -1;
			return;
		}
		if (x == grid.length) {
			ans[start] = y;
			return;
		}
		int cur = grid[x][y];
		if (direction == 0) {
			this.fall(grid, x, y + cur, cur, start, ans);
		} else {
			this.fall(grid, x + 1, y, 0, start, ans);
		}
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
